package api

import (
	"context"
	goods_pb "gitee.com/fangy1111/shop/goodsPb"
	"gitee.com/fangy1111/shop/userop"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"userop_bff/global"
	"userop_bff/model"
)

// 用户收藏列表
func GetFavList(c *gin.Context) {
	userId, _ := c.Get("uid")
	list, err := global.UserFavSrvConn.GetFavList(context.Background(), &userop.UserFavRequest{
		UserId: userId.(int32),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "获取收藏列表失败",
			"data": nil,
		})
		return
	}
	//声明一个用来存储商品id的欺骗
	var goodsIds []int32
	//把查到的商品id全部追加到goodsIds这个切片里面
	for _, v := range list.Data {
		goodsIds = append(goodsIds, v.GoodsId)
	}
	//跨微服务调用商品查询接口
	goods, err := global.GoodsSrvConn.BatchGetGoods(context.Background(), &goods_pb.BatchGoodsIdInfo{Id: goodsIds})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "获取商品信息失败",
			"data": nil,
		})
		return
	}

	var g []*model.Goods

	for _, v := range goods.Data {
		g = append(g, &model.Goods{
			Name:      v.Name,
			ShopPrice: v.ShopPrice,
		})
	}
	c.JSON(http.StatusBadRequest, gin.H{
		"code": 0,
		"msg":  "success",
		"data": map[string]interface{}{
			"data":  g,
			"total": list.Total,
		},
	})
}

// 添加用户收藏
func AddUserFav(c *gin.Context) {
	//这里少一步要先判断商品是否存在
	userId, _ := c.Get("uid")
	goodsId := c.PostForm("goods_id")
	goodsIdInt, _ := strconv.Atoi(goodsId)

	if len(goodsId) == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "商品id不能为空",
			"data": nil,
		})
		return
	}

	_, err := global.UserFavSrvConn.AddUserFav(context.Background(), &userop.UserFavRequest{
		UserId:  userId.(int32),
		GoodsId: int32(goodsIdInt),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "收藏商品失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": nil,
	})
}

// 删除用户收藏
func DeleteUserFav(c *gin.Context) {
	userId, _ := c.Get("uid")
	goodsId := c.PostForm("goods_id")
	goodsIdInt, _ := strconv.Atoi(goodsId)

	if len(goodsId) == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "商品id不能为空",
			"data": nil,
		})
		return
	}

	_, err := global.UserFavSrvConn.DeleteUserFav(context.Background(), &userop.UserFavRequest{
		UserId:  userId.(int32),
		GoodsId: int32(goodsIdInt),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "删除商品失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": nil,
	})
}

// 获取用户收藏详情
func GetUserFavDetail(c *gin.Context) {
	userId, _ := c.Get("uid")
	goodsId := c.Query("goods_id")
	goodsIdInt, _ := strconv.Atoi(goodsId)
	if len(goodsId) == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "商品id不能为空",
			"data": nil,
		})
		return
	}
	list, err := global.UserFavSrvConn.GetFavList(context.Background(), &userop.UserFavRequest{
		UserId:  userId.(int32),
		GoodsId: int32(goodsIdInt),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "获取商品详情失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": list,
	})
}
